#ifndef MACROFILEREADER
#define MACROFILEREADER

#include <fstream>
#include <vector>

#include "WinMacroLib.h"

class CMacroFileReader
{
private:
	bool VerifyFileSignature(std::ifstream &input)
	{
		char *file_signature = new char[signature_length];
		input.read(file_signature, signature_length);

		int retVal = strcmp(signature, file_signature);
		delete []file_signature;
		
		return retVal == 0;
	}

public:

	DWORD ReadMacroFile(LPCTSTR fileName, std::vector<EVENTMSG> &message_list)
	{
		std::ifstream input(fileName, std::ios::binary);

		if (VerifyFileSignature(input) == false)
		{
			return -1;
		}
		
		while (!input.eof())
		{
			EVENTMSG message;
			input.read((char *)&message, sizeof(EVENTMSG));
			message_list.push_back(message);
		}
		
		input.close();
		return 0;
	}

	DWORD ReadMacroFile(LPCTSTR fileName, EVENTMSG **messages, DWORD *message_count)
	{
		std::vector<EVENTMSG> message_list;

		DWORD retVal = ReadMacroFile(fileName, message_list);

		if (retVal != 0)
			return retVal;

		EVENTMSG *pMessage_array = new EVENTMSG[message_list.size()];
		EVENTMSG *pStart = pMessage_array;
		
		for(std::vector<EVENTMSG>::const_iterator iter = message_list.begin(); iter != message_list.end(); ++iter)
		{
			*pStart = *iter;
			pStart++;
		}

		*message_count = message_list.size();
		*messages = pMessage_array;
		return 0;
	}
};

#endif